package com.abu.edustatistic.controller;


import com.abu.edustatistic.pojo.StatisticsDaily;
import com.abu.edustatistic.service.StatisticsDailyService;
import com.abu.utils.Resp;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 网站统计日数据 前端控制器
 * </p>
 *
 * @author 阿布
 * @since 2020-06-25
 */
@RestController
@RequestMapping("/staservice/sta")
public class StatisticsDailyController {

    @Resource
    private StatisticsDailyService statisticsDailyService;

    @PostMapping("registerCount/{dateString}")
    public Resp saveSta(@PathVariable("dateString") String dateString) {
        Integer count = this.statisticsDailyService.saveSta(dateString);
        if (count == 1) {
            return Resp.ok();
        }
        return Resp.error();
    }

    /**
     * 每天一点获取最新数据添加到数据库中
     */
    @Scheduled(cron = "0 0 1 * * ?")        //定时任务方法不能有参数
    public void saveStaScheduled() {
        String lastDay = LocalDate.now().minusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        this.statisticsDailyService.saveSta(lastDay);
    }

//      http://localhost:8888/api/staservice/sta/showData/register_num/2020-06-26/2020-06-26

    @GetMapping("showData/{type}/{beginDate}/{endDate}")
    public Resp showStatistics(
            @PathVariable("type") String type,
            @PathVariable("beginDate") String beginDate,
            @PathVariable("endDate") String endDate) {
//        1.根据条件进行查询(返回结果接包括日期和对应的数量)
        List<StatisticsDaily> statisticsDailyList = this.statisticsDailyService.list(new QueryWrapper<StatisticsDaily>().between("date_calculated", beginDate, endDate).select("date_calculated", type));
//        2.前端需要json数组，所以要将结果集转换为list集合
//        遍历数据进行添加
        ArrayList<Integer> nums = new ArrayList<>();
        ArrayList<String> dates = new ArrayList<>();
        HashMap<String, Object> map = new HashMap<>();

        statisticsDailyList.forEach(statisticsDaily -> {
//       3.将符合条件所有的日期放入集合中
            dates.add(statisticsDaily.getDateCalculated());
//       4.将相应的数据放入集合中
            switch (type) {
                case "register_num":
                    nums.add(statisticsDaily.getRegisterNum());
                    break;
                case "login_num":
                    nums.add(statisticsDaily.getLoginNum());
                    break;
                case "video_view_num":
                    nums.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    nums.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }
            map.put("dataList", nums);
            map.put("dateList", dates);
        });
//        5.返回结果集
        return Resp.ok().data(map);
    }
}

